home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr14 / dndprogs.zip / NAMES.C < prev    next >
C/C++ Source or Header  |  1993-04-01  |  4KB  |  145 lines

  1. #include <stdio.h>
  2. #define MAXSYLLNUM 1024
  3. #define GETSYLL()    fscanf (dfile, "%s", linebuf);
  4. #define USAGE "Usage: names [-f file] [-n number-of-names] [-i]\n"
  5. char *progname;
  6.  
  7. #ifndef NAMEFILE
  8. # define NAMEFILE "default"
  9. /* "/userpk/etc/req/lib/names/default"
  10.                 /* default file for names */
  11. #endif
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char **argv;
  16.  
  17. {
  18.     extern char *optarg;
  19.     extern int optind, getopt();
  20.  
  21.     int  how_many,    /* number of names to generate        */
  22.          num_mids,    /*   "     " middle syllables (0-2)    */
  23.      seed = getpid ();    /* for random number generator        */
  24.     int  max_start = 0,    /* maximum numbers of start, middle and    */
  25.          max_mid = 0,    /* end syllables read  (subject to    */
  26.          max_end = 0;    /* ceiling of MAXSYLLNUM)          */
  27.  
  28.     char *name_start[MAXSYLLNUM];
  29.     char *name_middle[MAXSYLLNUM];
  30.     char *name_end[MAXSYLLNUM];
  31.     char linebuf[BUFSIZ];
  32.  
  33.     char *name_file = NAMEFILE;
  34.     FILE *dfile = NULL;    /* file pointer for data file */
  35.     int infinity = 0;    /* give an infinite number of names if set */
  36.     int option;
  37.  
  38.     progname = argv[0];
  39.  
  40.     /* parse args */
  41.  
  42.     while ( (option = getopt(argc, argv, "f:in:") )  != EOF) {
  43.     switch(option) {
  44.         case '?' :    /* no-such-option, getopt has printed a message */
  45.         fprintf(stderr, "%s", USAGE);
  46.         exit(1);
  47.         case 'f': {    /* -f file-of-syllables */
  48.         name_file = optarg;
  49.         break;
  50.         }
  51.         case 'n': {    /* how many names to do */
  52.         if ( (how_many = atoi(optarg)) < 0 ) {
  53.             err("number of names invalid (-n %d)\n", optarg);
  54.             exit(2);
  55.         }
  56.         break;
  57.         }
  58.         case 'i': {    /* infinite number of names */
  59.         infinity = 1;
  60.         break;
  61.         }
  62.  
  63.         default: {
  64.         fprintf(stderr, USAGE);
  65.         exit(2);
  66.         }
  67.     }
  68.     }
  69.     if (optind != argc) {
  70.     fprintf(stderr, USAGE);
  71.     exit(2);
  72.     }
  73.     if ((dfile = fopen (name_file, "r")) == NULL) {
  74.     err("cannot read database %s\n", name_file);
  75.     exit (2);
  76.     }
  77.  
  78.     /* read the data file */
  79.  
  80.     GETSYLL();
  81.     while (strcmp(linebuf, "%") != NULL && *linebuf) {
  82.     name_start[max_start] = (char *) malloc (strlen (linebuf) + 1);
  83.     strcpy (name_start[max_start++], linebuf);
  84.     if (max_start == MAXSYLLNUM) {
  85.         err("too many initial syllables - but continuing\n");
  86.         while (strcmp (linebuf, "%")) GETSYLL();
  87.         break;
  88.     } else GETSYLL();
  89.     }
  90.  
  91.     max_mid = 0;        /* initialise medial syllables */
  92.     GETSYLL();
  93.     while (strcmp (linebuf, "%") != NULL) {
  94.     name_middle[max_mid] = (char *) malloc (strlen (linebuf) + 1);
  95.     strcpy (name_middle[max_mid++], linebuf);
  96.     if (max_mid == MAXSYLLNUM) {
  97.         err("too many medial syllables - but continuing\n");
  98.         while (strcmp (linebuf, "%"))
  99.         GETSYLL();
  100.         break;
  101.     } else GETSYLL();
  102.     }
  103.  
  104.     max_end = 0;
  105.     GETSYLL();
  106.     while (strcmp (linebuf, "%") != NULL) {
  107.     name_end[max_end] = (char *) malloc (strlen (linebuf) + 1);
  108.     strcpy (name_end[max_end++], linebuf);
  109.     if (max_end == MAXSYLLNUM) {
  110.         err("too many final syllables - but continuing\n");
  111.         while (strcmp (linebuf, "%"))
  112.         GETSYLL();
  113.         break;
  114.     } else GETSYLL();
  115.     }
  116.  
  117.     /* now print the names out */
  118.  
  119.     if (!(max_start && max_mid && max_end)) {
  120.     err("zero-length section in datafile\n");
  121.     exit (3);
  122.     }
  123.  
  124.     srand (seed);
  125.  
  126.     while (infinity || how_many-- > 0) {
  127.     printf ("%s", name_start[rand_int () % max_start]);
  128.     for (num_mids = (rand_int () % 3) / 2; num_mids > 0; num_mids--)
  129.         printf ("%s", name_middle[rand_int () % max_mid]);
  130.     printf ("%s\n", name_end[rand_int () % max_end]);
  131.     }
  132. }
  133.  
  134. rand_int() {
  135.     return ((int) (rand() / 37));
  136. }
  137.  
  138. err(s, a1, a2, a3)
  139.     char *s;
  140. {
  141.     fputs(progname, stderr);
  142.     fputs(": ", stderr);
  143.     fprintf (stderr, s, a1, a2, a3);
  144. }
  145.